<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const arr = [
            { id: 1, name: "西瓜", state: true, price: 10, count: 100},
            { id: 2, name: "香蕉", state: true, price: 120, count: 100 },
            { id: 3, name: "菠萝", state: true, price: 20, count: 100 },
            { id: 4, name: "草莓", state: false, price: 90, count: 100 },
        ]
        // 计算 state 为 true 的水果的价钱
        // let SunPrice = 0
        // arr.forEach(item => {
        //     if(item.state){
        //         SunPrice += item.price
        //         // console.log(item.price)
        //     }
        // })
        // console.log(SunPrice)

        // 数组过滤  arr.filter(item => item.state) 返回值是一个过滤的数组
        // reduce 的使用 reduce((累加的结果，当前循环项) => {}, 初始值)
        const result = arr.filter(item => item.state).reduce((amt,item) => {
            return amt += item.price * item.count
        }, 0)
        console.log(result)
        // console.log(arr.filter(item => item.state))
    </script>

</body>
</html>